package com.example.programexpert

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import java.util.Locale

class FifthActivity : ComponentActivity() {
    var seconds = 0
    var running = false
    var wasRunning = false

    private lateinit var textViewTimer: TextView
    private lateinit var handler: Handler
    private lateinit var runnable: Runnable

    companion object {
        private const val TAG = "Life cycle"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fifth)

        val buttonStart = findViewById<Button>(R.id.button4)
        val buttonStop = findViewById<Button>(R.id.button5)
        val buttonRestart = findViewById<Button>(R.id.button6)
        textViewTimer = findViewById(R.id.textView_timer)

        Log.d(TAG, "onCreate")
        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }

        // 初始化 Handler 和 Runnable
        handler = Handler(Looper.getMainLooper())
        runnable = object : Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60
                // 格式化为 HH:MM:SS
                textViewTimer.text =
                    String.format(Locale.US, "%02d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000) // 每隔 1000ms（1秒）再次调用自己
            }
        }

        runTimer() // 启动计时器显示

        buttonStart.setOnClickListener {
            running = true
        }

        buttonStop.setOnClickListener {
            running = false
        }

        buttonRestart.setOnClickListener {
            running = true
            seconds = 0
        }
    }

    private fun runTimer() {
        handler.post(runnable) // 启动定时器：第一次调用 runnable
    }

    // 保存状态
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d(TAG, "onSaveInstanceState")
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        // 移除所有回调，避免内存泄漏
        handler.removeCallbacks(runnable)
        Log.d(TAG, "onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")
        if (wasRunning) {
            running = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
        wasRunning = running
        running = false
    }
}